{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Insertion Sort"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def insertion_sort(array):\n",
    "    global iterations\n",
    "    iterations = 0\n",
    "    for i in range(1, len(array)):\n",
    "        current_value = array[i]\n",
    "        for j in range(i - 1, -1, -1):\n",
    "            iterations += 1\n",
    "            if array[j] > current_value:\n",
    "                array[j], array[j + 1] = array[j + 1], array[j] # swap\n",
    "            else:\n",
    "                array[j + 1] = current_value\n",
    "                break"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Time Complexity:\n",
    "\n",
    "- Best Case: O(n)\n",
    "- Average Case: O(n * n)\n",
    "- Worst Case:  O(n * n)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Code for executing and seeing the difference in time complexities"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Best Case Performance:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n",
      "18\n"
     ]
    }
   ],
   "source": [
    "# elements are already sorted\n",
    "array = [i for i in range(1, 20)]\n",
    "\n",
    "insertion_sort(array)\n",
    "# 20 ALREADY sorted elements need 18 iterations approx = n\n",
    "print(array)\n",
    "print(iterations)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Average Case Performance:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n",
      "89\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "# elements are randomly shuffled\n",
    "array = [i for i in range(1, 20)]\n",
    "random.shuffle(array)\n",
    "\n",
    "insertion_sort(array)\n",
    "# 20 shuffled elements need 324 iterations approx = n * n\n",
    "print(array)\n",
    "print(iterations)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Worst Case Performance:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n",
      "171\n"
     ]
    }
   ],
   "source": [
    "# elements are reverse sorted\n",
    "array = [i for i in range(1, 20)]\n",
    "# reversing the array\n",
    "array = array[::-1]\n",
    "\n",
    "insertion_sort(array)\n",
    "# 20 REVERSE sorted elements need 324 iterations approx = n * n\n",
    "\n",
    "print(array)\n",
    "print(iterations)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Applications"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "More efficient that other sorts when N is comparatively smaller, say < 30."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
